package com.pattern.structure.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangguanzan
 * @date 7/2/22
 * @description
 */
public class CompositeTree implements CompositeComponent {

    private List<CompositeComponent>  nodeList;


    public CompositeTree(){
        nodeList=new ArrayList<>();
    }

    @Override
    public void add(CompositeComponent treeNode) {
        nodeList.add(treeNode);
    }

    @Override
    public void operation() {
        System.out.println("CompositeTree doing");
        for(CompositeComponent comonet: nodeList){
            comonet.operation();
        }
    }

    @Override
    public void remove(CompositeComponent leaf) {
        nodeList.remove(leaf);
    }

    @Override
    public CompositeComponent getChild(int i) {
        return nodeList.get(i);
    }
}
